package com.study.service.impl;

import com.study.entity.Person;
import com.study.repository.PersonRepository;
import com.study.service.Neo4jService;
import org.neo4j.driver.Driver;
import org.neo4j.driver.Record;
import org.neo4j.driver.Result;
import org.neo4j.driver.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static org.neo4j.driver.Values.parameters;

@Service
public class Neo4jServiceImpl implements Neo4jService {

    @Autowired
    private PersonRepository repository;
    @Autowired
    private Driver driver;

    public List<Person> findChildList(String id) {
        System.out.println("id : " + id);
        List<Person> childList = repository.findChildList(id);
        for (Person person : childList) {
            System.out.println("person = " + person.toString());
        }
        return childList;
    }

    @Override
    public boolean createPerson() {
        Session session = driver.session();

        session.run("CREATE (n:Person {name:$name, age:$age})",
                parameters("name", "妮妮同学", "age", 20));
        session.run("CREATE (n:Person {name:$name, age:$age})",
                parameters("name", "想想同学", "age", 24));
        session.run("CREATE (n:Person {name:$name, age:$age})",
                parameters("name", "叙同学", "age", 18));


        Result result = session.run("MATCH (n:Person) WHERE n.name = $name " +
                        "RETURN n.name as name, n.age as age",
                parameters("name", "妮妮同学"));
        while (result.hasNext()) {
            Record record = result.next();
            System.out.println(record.get("name") + ":" + record.get("age"));
        }
        session.close();
        return true;
    }

}
